//
//  JiaZhenViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JiaZhenViewController.h"

#import "GXCell.h"
#import "JiaZDetViewController.h"
@interface JiaZhenViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation JiaZhenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"家政"];
    array = [NSMutableArray array];
    [self loadUI];
    [self loadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    GXCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GXCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array[indexPath.row];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"figurepic"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM截图20170819150938"]];
    cell.labTit.text = [dic objectForKey:@"name"];
    cell.labDet.text = [dic objectForKey:@"profile"];
    
    NSString *km = [NSString stringWithFormat:@" %likm",[[dic objectForKey:@"km"] integerValue]];
    NSString *order = [NSString stringWithFormat:@"%li ",[[dic objectForKey:@"order"] integerValue]];
    NSString *count = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"countcomment"] integerValue]];
    
    [cell.btnL setTitle:km forState:UIControlStateNormal];
    [cell.btnC setTitle:order forState:UIControlStateNormal];
    [cell.btnR setTitle:count forState:UIControlStateNormal];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 154.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JiaZDetViewController *det = [[JiaZDetViewController alloc] init];
    NSDictionary *dic = array[indexPath.row];
    det.merid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    det.name = [dic objectForKey:@"name"];
    det.det = [dic objectForKey:@"profile"];
    det.place = [dic objectForKey:@"suite"];
    det.order = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"order"] integerValue]];
    det.star = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"star"] integerValue]];
    
    [self.navigationController pushViewController:det animated:YES];
}
//家政列表
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lat forKey:@"longitude"];
    [parmas setObject:_lng forKey:@"latitude"];
    [parmas setObject:@"1" forKey:@"p"];
    NSLog(@"parmas%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/merchant",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政res:%@",responseObject);
        
        array = [responseObject objectForKey:@"result"];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
